package com.mopon.test.tx.util;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import com.mopon.test.tx.service.BusinessService;
import com.mopon.test.tx.service.impl.BusinessServiceImpl;

public class BeanFactory {

	public static BusinessService getBusinessService(){
        final BusinessService s = new BusinessServiceImpl();

        BusinessService proxyS = (BusinessService)Proxy.newProxyInstance(s.getClass().getClassLoader(), 
                s.getClass().getInterfaces(), 
                new InvocationHandler() {
                    public Object invoke(Object proxy, Method method, Object[] args)
                            throws Throwable {
                        try {
                            TransactionManager.startTransaction();
                            Object rtValue = method.invoke(s, args);
                            return rtValue;
                        } catch (Exception e) {
                            TransactionManager.rollback();
                            throw new RuntimeException(e);
                        } finally {
                            TransactionManager.commit();
                            TransactionManager.release();
                        }
                    }
                });

        return proxyS;
    }
}
